<?php
  // no direct access
  defined( '_JEXEC' ) or die( 'Restricted access' );
  jimport('joomla.application.component.controller');
  include('base.php');

  class TeamGameStatController extends BaseController
  {
    var $_name = 'teamgamestat';
    
    function display()
    {
      $document =& JFactory::getDocument();
      
      $view = & $this->getView($this->_name, $document->getType());
      $view->setModel($this->getModel($this->_name), true);
      
      $view->displayList();
    }
    
    function add()
    {
      $document =& JFactory::getDocument();
      
      $view = & $this->getView($this->_name, $document->getType());
      $view->setModel($this->getModel($this->_name), true);
      $view->setModel($this->getModel('team','teammodel'));
      $view->setModel($this->getModel('schedule','schedulemodel'));
      
      $view->displayAdd();
    }
    
    function edit()
    {
      $cids = JRequest::getVar('cid', null, 'default', 'array' ); 
      if($cids === null)
          JError::raiseError(500, 'cid parameter missing from the request');
      
      $document =& JFactory::getDocument();
      
      $view = & $this->getView($this->_name, $document->getType());
      $view->setModel($this->getModel($this->_name), true);
      $view->setModel($this->getModel('team','teammodel'));
      $view->setModel($this->getModel('schedule','schedulemodel'));
      $view->setModel($this->getModel('venue','venuemodel'));
      
      $view->displayEdit();  
    }
    
    
    function ajx_select_scheds()
    {
      $team_id     = JRequest::getVar('team_id');
      $schedule    = $this->getModel('schedule','schedulemodel');
      $team        = $this->getModel('team','teammodel');
      $venue       = $this->getModel('venue', 'venuemodel');
      
      $result      = array();
      $schedules   = $schedule->get_by_team_id($team_id);
      if($schedules){
        foreach($schedules as $schedule){
          $old_data           = $schedule->schedule;
          $team1              = $team->get($schedule->first_team_id)->name;
          $team2              = $team->get($schedule->second_team_id)->name;
          $court              = $venue->get($schedule->venue_id)->name;
          $schedule->schedule = "$old_data   $team1 vs. $team2 @ $court";
        }
        $result = $schedules;
      }
      
      $result  = JHTML::_('select.genericlist', $result, 'schedule_id', null, 'id', 'schedule', null);
      echo $result;
    }
  }
?>
